package com.brooks.demo;

import java.util.concurrent.TimeUnit;

/**
 * 有异常锁会释放
 *
 * @Author mihutu
 * @Date 2020/10/16 15:12
 * @Version 1.0
 */
public class DemoThread8 implements Runnable {
    private int n = 0;

    @Override
    public synchronized void run(){
        System.out.println(Thread.currentThread().getName()+"开始执行");
        for (int i=0;i<100;i++) {
            n++;
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (i == 5) {
                i = i/0;
                System.out.println(i);
            }
            System.out.println(n);
        }
    }

    public static void main(String[] args) {
        DemoThread8 t = new DemoThread8();
        new Thread(t,"thread1").start();
        new Thread(t,"thread2").start();
    }
}